package game.entity;

import game.shared.Constants;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class SpriteManager{
	ArrayList<AbsSprite> listSprite;
	Sound s;
	public SpriteManager() {
		listSprite = new ArrayList<AbsSprite>();
//		s = Gdx.audio.newSound(Gdx.files.internal("res/sound/electricshock.wav"));
	}
	
	public void render(SpriteBatch sb){
		for(int i = 0; i<listSprite.size(); i++){
			listSprite.get(i).render(sb);
		}
	}
	public void renderShaper(){
		for(int i=0; i<listSprite.size(); i++){
			listSprite.get(i).renderShape();
		}
	}
	
	public void update(){
		int n = listSprite.size();
		for(int i=0; i<n; i++)
		{
			AbsSprite sprite = listSprite.get(i);
			sprite.update();
			if(sprite.getHp() <= 0)
			{
				this.removeById(sprite.getID());
				i--; n--;
				if(sprite instanceof Monster)
					Constants.GOLD += 5;
			}
		}
	}
	
	public void add(AbsSprite sprite)
	{
		listSprite.add(sprite);
	}
	
	public void removeAll()
	{
		listSprite.removeAll(listSprite);
	}
	
	public void remove(int index){
		if(index < listSprite.size())
			listSprite.remove(index);
	}
	
	public void removeById(int id){
		for(int i = 0; i<listSprite.size(); i++)
			if(listSprite.get(i).getID() == id)
				listSprite.remove(i);
	}
	
	public AbsSprite getByID(int id)
	{
		for (AbsSprite sprite : listSprite) {
			if(sprite.getID() == id)
				return sprite;
		}
		return null;
	}
	
	public AbsSprite getByIndex(int index)
	{
		if(index >= 0)
			if(index < listSprite.size())
				return listSprite.get(index);
		return null;
	}

	public int count() {
		return listSprite.size();
	}
	
	public void pause()
	{
		for (AbsSprite sprite : listSprite)
			sprite.setbPause(true);
	}
	
	public void resume()
	{	
		for (AbsSprite sprite : listSprite)
			sprite.setbPause(false);
	}
}
